
//
//  ExercisesPageModel.m
//  ShowYork
//
//  Created by hxh on 2020/9/8.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "ExercisesPageModel.h"
#import "ExercieseQuestionItemModel.h"




@implementation ExercisesPageModel
MJCodingImplementation


- (NSMutableArray *)objects{
    if (!_objects) {
        _objects = [NSMutableArray array];
    }
    return _objects;
}

+(NSDictionary *)mj_objectClassInArray{
    return @{@"titleItems":[ExercisesTitleModel class]};
}


- (id)copyWithZone:(nullable NSZone *)zone{
    __typeof(self) obj = [[self class] new];
    unsigned int count = 0;
    Ivar *list = class_copyIvarList([self class], &count);
    for (int i = 0; i < count; i++) {
        NSString *type = [NSString stringWithCString:ivar_getTypeEncoding(list[i]) encoding:NSUTF8StringEncoding];
        if([type hasPrefix:@"@"]){
            object_setIvar(obj, list[i], object_getIvar(self, list[i]));
        }
    }
    free(list);
    return obj;
}

- (NSArray *)errorAndTitleArray{
    NSMutableArray *res = [NSMutableArray array];
    
    [self.objects enumerateObjectsUsingBlock:^(ExercieseQuestionItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (!obj.answerItem.doRight && obj.type.intValue != 5) {
            [res addObject:obj];
        }
    }];
    return res;
}

- (NSArray *)errorArray{
    
    NSMutableArray *res = [NSMutableArray array];
    
    [self.objects enumerateObjectsUsingBlock:^(ExercieseQuestionItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (!obj.answerItem.doRight && (obj.type.intValue != 5 && obj.type.intValue > 0)) {
            [res addObject:obj];
        }
    }];
    return res;
}


- (NSArray *)resultArray{
    
    if (self.onlyQuestion) {
        return self.allArray;
    }
    if (self.onlyError) {
        return self.errorArray;
    }
    if (self.errorQuestion) {
        return self.errorAndTitleArray;
    }
    return self.objects;

}

- (BOOL)existError{
    return self.errorArray.count > 0;
}

- (NSArray *)allArray{
    NSMutableArray *res = [NSMutableArray array];
    [self.objects enumerateObjectsUsingBlock:^(ExercieseQuestionItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.type.intValue != 0) {
            [res addObject:obj];
        }
    }];
    return res;
}

- (void)save{
    
    NSData * data  = [NSKeyedArchiver archivedDataWithRootObject:self];

    
    NSString *key = [NSString stringWithFormat:@"%@/%@",[RPAppManager getCustomerId],self.id];
    
    [[NSUserDefaults standardUserDefaults]setObject:data forKey:key];
    
    [[NSUserDefaults standardUserDefaults]synchronize];
    
}

+ (ExercisesPageModel *)getCurrentExercieseById:(NSNumber *)id{
    
    NSString *key = [NSString stringWithFormat:@"%@/%@",[RPAppManager getCustomerId],id];
      
    NSData *data = [[NSUserDefaults standardUserDefaults]objectForKey:key];
    
    ExercisesPageModel *model = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    
    return model;
    
}

+ (void)deleteExercieseById:(NSNumber *)id{
    
    NSString *key = [NSString stringWithFormat:@"%@/%@",[RPAppManager getCustomerId],id];
       
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:key];
    [[NSUserDefaults standardUserDefaults]synchronize];
}

- (void)modifyObject{
    NSMutableArray *res = [NSMutableArray array];
    __block NSInteger index = 1;

    [self.titleItems enumerateObjectsUsingBlock:^(ExercisesTitleModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
       // 首先包含一个对象
        ExercieseQuestionItemModel *model = [[ExercieseQuestionItemModel alloc]init];
        model.paperName = _name;
        model.subName = obj.name;
        model.score = obj.totalScore;
        model.count = @(obj.questionItems.count);
        model.totalCount = _questionCount;
        model.paperId = _id;
        [res addObject:model];
        
        [obj.questionItems enumerateObjectsUsingBlock:^(ExercisesQuqestionModel * _Nonnull item, NSUInteger idx, BOOL * _Nonnull stop) {
            ExercieseQuestionItemModel *nextModel = [[ExercieseQuestionItemModel alloc]init];
            nextModel.analyze = item.analyze;
            nextModel.score = item.score;
            nextModel.correct = item.correct;
            nextModel.analyzeAttr = item.analyzeAttr;
            nextModel.titleAttr = item.titleAttr;
            nextModel.correctAttr = item.correctAttr;
            nextModel.correctArray = item.correctArray;
            nextModel.type = item.questionType;
            nextModel.title = item.title;
            nextModel.id = item.id;
            nextModel.subjectId = item.subjectId;
            nextModel.currentIndex = @(index);
            nextModel.totalCount = _questionCount;
            nextModel.items = item.items;
            nextModel.answerItem = item.answerItem;
            nextModel.paperId = _id;
            nextModel.paperName = _name;
            nextModel.subName = obj.name;
            nextModel.questionIndex = idx;
            [res addObject:nextModel];
            index++;
        }];
    }];
    
    [self.objects addObjectsFromArray:res];
}
- (NSInteger)totalScore{
    __block NSInteger score = 0;
    [self.objects enumerateObjectsUsingBlock:^(ExercieseQuestionItemModel *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.type.intValue > 0) {
            score += obj.score.intValue;
        }
    }];
    return score;
}



@end
